package com;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;

public class RedisSDIU {

	private static Jedis jedis = new Jedis("localhost");

	public static void selectAllKey() {
		// 获取全部键值
		Set<String> keys = jedis.keys("*");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next();
			System.out.println(key);
		}
	}

	public static void select(String key) {
		// 获取单个字符串
		System.out.println(jedis.get(key));
	}

	public static void selectMap(String key) {
		// 获取map
		Map<String, String> map = jedis.hgetAll(key);
		Set<String> set = map.keySet();
		for (String s : set) {
			System.out.println(s + ": " + map.get(s));
		}
	}

	public static void delete(String key) {
		// 删除数据
		jedis.del(key);
		System.out.println("删除成功");
	}

	// 添加单个字符串
	public static void add(String key, String value) {
		jedis.set(key, value);
		System.out.println("添加成功");
	}

	public static void addMap(String key, Map<String, String> map) {
		// 添加一个map到redis中
		Set<String> mkeys = map.keySet();
		for (String mkey : mkeys) {
			jedis.hset(key, mkey, map.get(mkey));
		}
		System.out.println("添加成功");
	}

	public static void main(String[] args) {
		jedis.auth("cyf13209202523");
		System.out.println("连接成功");

		// 获取全部的键值
		// selectAllKey();

		// 添加单个字符穿
		// add("test", "测试");
		// select("test");

		// 删除数据
		// delete("user");

		// 添加一个map，并且进行查找
		// Map<String, String> map = new HashMap<String, String>();
		// map.put("name", "炊逸飞");
		// map.put("age", "23");
		// map.put("sex", "男");
		// map.put("phone", "18000000000");
		// addMap("user", map);
		// selectMap("user");

		// 返回当前键值存储值的类型
		// System.out.println(jedis.type("user"));
	}
}
